package app

import (
	"fmt"
	"gitee.com/ling1314/origin-utility/config"
	"gitee.com/ling1314/origin-utility/db"
	"gitee.com/ling1314/origin-utility/miao_ser"
	"github.com/gin-gonic/gin"
	"github.com/kardianos/service"
)

var app *App

type App struct {
	Server *gin.Engine
}

func init() {
	app = &App{}
}

func Run() error {
	// 初始化数据库
	db.InitDB()
	// 数据迁移
	db.Run()
	// 加载缓存
	miao_ser.LoadIdpCatch()
	// 初始化tcp服务监听
	miao_ser.InitTcpServer()
	// 启动http服务
	app.Server = setupRoutes()
	if config.GlobalCfg.Server.Cert != "" && config.GlobalCfg.Server.Key != "" {
		return app.Server.RunTLS(config.GlobalCfg.Server.Addr, config.GlobalCfg.Server.Cert, config.GlobalCfg.Server.Key)
	} else {
		return app.Server.Run(config.GlobalCfg.Server.Addr)
	}
}

func (a App) Start(s service.Service) error {
	fmt.Println("coming Start.......")

	return nil
}

func (a App) Stop(s service.Service) error {
	fmt.Println("coming Start.......")
	return s.Stop()
}
